import smbus
import time

# i2c 具体读写实现

#旧版RPI bus = smbus.SMBus(0)
bus = smbus.SMBus(1)

# 通过sudo i2cdetect -y -1 可以获取到i2c的地址
def setup(Addr):
    global address
    address = Addr

def read(chn):  #通道选择 此芯片的输入通道范围0-3
    try:
        if chn==0:
            bus.write_byte(address,0x40)
        if chn==1:
            bus.write_byte(address,0x41)
        if chn==2:
            bus.write_byte(address,0x42)
        if chn==3:
            bus.write_byte(address,0x43)
        bus.read_byte(address)
    except Exception as e:
        print("Address is %s" % address)
        print(e)
    return bus.read_byte(address)

def write(val):
    try:
        temp=val
        temp=int(temp)
        bus.write_byte_data(address,0x40,temp)
    except Exception as e:
        print("Error:device is 0x%2x"%address)
        print(e)

if __name__=='__main__':
    pass
    # 写完库的时候测试库用的